<template>
  <div>
    <el-dialog
      @close="dialogClose"
      title="地址详细"
      :visible.sync="dialogTableVisible"
    >
      <el-form ref="form" :rules="rules" :model="form" label-width="140px">
        <el-form-item prop="addressName" label="发货地址">
          <el-input v-model="form.addressName"></el-input>
        </el-form-item>
        <el-form-item label="发货人姓名">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="发货人手机号">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item label="邮政编码">
          <el-input v-model="form.email"></el-input>
        </el-form-item>
        <el-form-item label="收货地址">
          <el-cascader
            size="large"
            :options="options"
            v-model="form.address"
            @change="handleChange"
          >
          </el-cascader>
        </el-form-item>
        <el-form-item label="详细地址">
          <el-input v-model="form.detailAddress"></el-input>
        </el-form-item>
        <el-form-item label="是否默认收货地址">
          <el-switch v-model="form.receiveStatus"></el-switch>
        </el-form-item>
        <el-form-item label="是否发货收货地址">
          <el-switch v-model="form.sendStatus"></el-switch>
        </el-form-item>
        <el-form-item size="large">
          <el-button v-if="btnshow" type="primary" @click="onSubmit"
            >确认</el-button
          >
          <el-button v-else type="primary" @click="onSubmit1">确认</el-button>
          <el-button @click="change">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { regionData, CodeToText } from "element-china-area-data";
import addressApi from "@/api/order/address/index";
export default {
  components: {},

  data() {
    return {
      options: regionData,
      selectedOptions: [],
      dialogTableVisible: false,
      form: {
        addressName: "",
        name: "",
        phone: "",
        email: "",
        detailAddress: "",
        receiveStatus: false,
        sendStatus: false,
        address: "",
      },
      btnshow: true,
      tofather: true,
      rules: {
        addressName: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
        ],
      },
    };
  },

  created() {},
  props: ["sonData"],
  mounted() {},

  methods: {
    dialogClose() {
      console.log("object");
      // this.form=''
    },
    handleChange(e) {
      console.log(e);
    },
    //   切换显示与隐藏
    change() {
      this.dialogTableVisible = !this.dialogTableVisible;
      console.log(this.sonData);
      this.$nextTick(() => {
        if (this.sonData.id) {
          this.form = this.sonData;
          this.btnshow = false;
        }
      });
    },
    change1() {
      this.form = {
        addressName: "",
        name: "",
        phone: "",
        email: "",
        detailAddress: "",
        receiveStatus: false,
        sendStatus: false,
        address: "",
      };
      this.btnshow = true;
      this.dialogTableVisible = !this.dialogTableVisible;
    },
    // 编辑地址的提交
    onSubmit1() {
      console.log("111");
      var obj = JSON.parse(JSON.stringify(this.form));
      obj.receiveStatus = obj.receiveStatus ? 1 : 0;
      obj.sendStatus = obj.sendStatus ? 1 : 0;
      addressApi.update(obj).then((res) => {
        console.log(res);
        if (res.success) {
          this.$message({
            showClose: true,
            message: "修改成功",
            type: "success",
          });
          this.dialogTableVisible = !this.dialogTableVisible;
          this.$refs.form.resetFields();
          this.$emit("tofather", this.tofather);
        }
      });
    },
    //   新增的提交数据
    onSubmit() {
      console.log("111");
      var obj = JSON.parse(JSON.stringify(this.form));
      obj.receiveStatus = obj.receiveStatus ? 1 : 0;
      obj.sendStatus = obj.sendStatus ? 1 : 0;
      addressApi.save(obj).then((res) => {
        console.log(res);
        if (res.success) {
          this.$message({
            showClose: true,
            message: "添加成功",
            type: "success",
          });
          this.dialogTableVisible = !this.dialogTableVisible;
          this.$refs.form.resetFields();
          this.$emit("tofather", this.tofather);
        }
      });
    },
  },

  computed: {},

  watch: {},
};
</script>

<style scoped lang='scss'>
</style>